Auth = function(){};
Auth.Capabilite = function(){};
Auth.Capabilite.add = function(id,name){
    if(Auth.Capabilite.find(id)){
        alert('Desculpe, essa capacidade já está associada a essa regra.');
    } else {
        var capabilites = document.getElementById("capabilite_ids");
        if(capabilites.value == ""){
           capabilites.value = id; 
        } else {
            capabilites.value = capabilites.value + "," +id;
        }
                
        capabilites = document.getElementById("capabilites");
        var html = capabilites.innerHTML;
        html += "<li id='capabilites_li_"+id+"'>"+name+" <a href=\"javascript:Auth.Capabilite.remove('"+id+"')\"><img src='/images/icons/remove.gif' border=\"0\" /></a></li>";
        capabilites.innerHTML = html;
    }
};

Auth.Capabilite.load = function(id,name){
    var capabilites = document.getElementById("capabilites");
    var html = capabilites.innerHTML;
    html += "<li id='capabilites_li_"+id+"'>"+name+" <a href=\"javascript:Auth.Capabilite.remove('"+id+"')\"><img src='/images/icons/remove.gif' border=\"0\" /></a></li>";
    classifications.innerHTML = html;
};

Auth.Capabilite.remove = function(id){
    var capabilites = document.getElementById("capabilite_ids");
    var capabilites_ids = capabilites.value.split(",");
    var ids = "";
    for(i=0;i<capabilites_ids.length;i++){
        if(capabilites_ids[i] != id){
            if(ids.length == 0){
                ids = capabilites_ids[i];
            } else {
                ids += ","+capabilites_ids[i];
            }
        }
    }
    
    capabilites.value = ids;
    
    capabilites = document.getElementById("capabilites");
    var capabiliteLI = document.getElementById("capabilites_li_"+id);
    capabilites.removeChild(capabiliteLI);
    
};

Auth.Capabilite.find = function(id){
    var capabilites = document.getElementById("capabilite_ids").value.split(",");
    for(i=0;i<capabilites.length;i++){
        if(capabilites[i] == id){
            i = capabilites.length;
            return true;
        }
    }
    return false;
};

/*
Script para adição de Regras ao um determinado Grupo
*/
Auth.Role = function(){};
Auth.Role.add = function(id,name){
    if(Auth.Role.find(id)){
        alert('Desculpe, essa regra já está associada a esse grupo.');
    } else {
        var ids = document.getElementById("role_ids");
        if(ids.value == ""){
           ids.value = id; 
        } else {
            ids.value = ids.value + "," +id;
        }
        list = document.getElementById("roles");
        var html = list.innerHTML;
        html += "<li id='role_li_"+id+"'>"+name+" <a href=\"javascript:Auth.Role.remove('"+id+"')\"><img src='/images/icons/remove.gif' border=\"0\" /></a></li>";
        list.innerHTML = html;
    }
};

Auth.Role.remove = function(id){
    var ids = document.getElementById("role_ids").value.split(",");
    var idsAfterRemove = "";
    for(i=0;i<ids.length;i++){
        if(ids[i] != id){
            if(idsAfterRemove.length == 0){
                idsAfterRemove = ids[i];
            } else {
                idsAfterRemove += ","+ids[i];
            }
        }
    }
    document.getElementById("role_ids").value = idsAfterRemove;
    list = document.getElementById("roles");
    var li = document.getElementById("role_li_"+id);
    list.removeChild(li);    
};

Auth.Role.find = function(id){
    var ids = document.getElementById("role_ids").value.split(",");
    for(i=0;i<ids.length;i++){
        if(ids[i] == id){
            i = ids.length;
            return true;
        }
    }
    return false;
};

/*
Script para adição de Usuário ao um determinado Grupo
*/
Auth.User = function(){};
Auth.User.add = function(id,name){
    if(Auth.User.find(id)){
        alert('Desculpe, esse usuário já está associado a esse grupo.');
    } else {
        var ids = document.getElementById("user_ids");
        if(ids.value == ""){
           ids.value = id; 
        } else {
            ids.value = ids.value + "," +id;
        }
        list = document.getElementById("users");
        var html = list.innerHTML;
        html += "<li id='user_li_"+id+"'>"+name+" <a href=\"javascript:Auth.User.remove('"+id+"')\"><img src='/images/icons/remove.gif' border=\"0\" /></a></li>";
        list.innerHTML = html;
    }
};

Auth.User.remove = function(id){
    var ids = document.getElementById("user_ids").value.split(",");
    var idsAfterRemove = "";
    for(i=0;i<ids.length;i++){
        if(ids[i] != id){
            if(idsAfterRemove.length == 0){
                idsAfterRemove = ids[i];
            } else {
                idsAfterRemove += ","+ids[i];
            }
        }
    }
    document.getElementById("user_ids").value = idsAfterRemove;
    list = document.getElementById("users");
    var li = document.getElementById("user_li_"+id);
    list.removeChild(li);    
};

Auth.User.find = function(id){
    var ids = document.getElementById("user_ids").value.split(",");
    for(i=0;i<ids.length;i++){
        if(ids[i] == id){
            i = ids.length;
            return true;
        }
    }
    return false;
};
